package cn.tedu._05egweibo01.controller;

import cn.tedu._05egweibo01.common.response.JsonResult;
import cn.tedu._05egweibo01.common.response.StateCode;
import cn.tedu._05egweibo01.mapper.WeiboMapper;
import cn.tedu._05egweibo01.pojo.dto.WeiboDTO;
import cn.tedu._05egweibo01.pojo.entity.Weibo;
import cn.tedu._05egweibo01.pojo.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "微博模块")
@RestController
@RequestMapping("v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发表评论功能")
    @PostMapping("insert")
    public JsonResult insertWeibo(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.debug("weiboDTO = " + weiboDTO + ", session = " + session);
        UserVo user = (UserVo) session.getAttribute("user");
//        if (user == null){ //用户未登录
//            return new JsonResult(StateCode.NOT_LOGIN);
//        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(user.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }

    @ApiOperation("微博首页列表接口")
    @GetMapping("selectIndex")
    public JsonResult seletIndex(){
        return JsonResult.ok(weiboMapper.selectIndex());
    }

    @ApiOperation("微博详情页")
    @ApiImplicitParam(name = "id",value = "微博Id",dataType = "long",required = true)
    @GetMapping("selectById")
    public JsonResult selectByID(Long id){
        return JsonResult.ok(weiboMapper.selectById(id));
    }
}
